package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTable;
import javax.swing.JTextArea;

import controller.DataBaseInteactor;

import model.Conference;

import model.SubProgramChair;
/**
 * The view of the user when he/she login as SubProgram Chair role.
 * 
 * @author Richard Lee
 * @version 52813
 */
public class SubProgramChairView extends JFrame {

	/**
	 * Default serial version id.
	 */
	private static final long serialVersionUID = 9159054343029035132L;

	/**
	 * The conference.
	 */
	private Conference conference;
	
	/**
	 * GUI component.
	 */
	private final Font titleFont = new Font("Verdana", Font.BOLD, 20);
	private final JLabel titleLabel = new JLabel();
	private final JLabel dateLabel = new JLabel();
	private final JPanel buttonsPanel = new JPanel(new FlowLayout());
	private final JPanel recomPanel = new JPanel(new GridLayout(2, 2));
	private final JButton submitButton = new JButton("Submit Recommendation");
	private final JButton assignReviewerButton = new JButton("Assign Reviewer");
	private final JButton logoutButton = new JButton("Log out");
	private final JButton viewButton = new JButton("View");
	private final JPanel titlePanel = new JPanel(new BorderLayout());
	private final String id;
	private JTextArea summaryArea = new JTextArea();
	private Object[][] manuscriptData = {{"Title1", "NOT REVIEWED", "Nick", "YES", "NO"},
			{"Title2", "ACCEPTED", "Yang", "YES", "YES"}};
	private JTable manTabel;
	private ButtonGroup bg;
	
	/**
	 * Database interactor.
	 */
	private DataBaseInteactor inteactor;
	
	/**
	 * Constructor.
	 * @param theConf the conference user belongs.
	 * @param theInteactor database interactor.
	 * @param theId user id.
	 */
	public SubProgramChairView(Conference theConf, DataBaseInteactor theInteactor, String theId) {
		super("SubProgram Chair View");
		conference = theConf;
		inteactor = theInteactor;
		id = theId;
	}
	
	/**
	 * Add and combein all the GUI components.
	 */
	public void creatSubProgramChairView() {
		JPanel p = new JPanel();
		p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
		StringBuilder sb = new StringBuilder();
		sb.append("Manuscript Due Date: ");
		sb.append(conference.getManDate());
		sb.append(" Review Due Date: ");
		sb.append(conference.getreviewDate());
		sb.append(" Recommendation Due Date: ");
		sb.append(conference.getRecomDate());
		dateLabel.setText(sb.toString());
		titleLabel.setText(conference.getName());
		titleLabel.setFont(titleFont);
		titlePanel.add(titleLabel, BorderLayout.CENTER);
		titlePanel.add(dateLabel, BorderLayout.PAGE_END);
		p.add(titlePanel);
		String[] columnNames = {"Title", "Stauts", "Reviewer", "Review", "Recomendation"};

		manTabel = new JTable(manuscriptData, columnNames);
		
		recomPanel.add(new JLabel("Summary Recoommendation"));
		bg = new ButtonGroup();
		JPanel radioPanel = new JPanel();
		for (int i = 1; i < 6; i++) {
			JRadioButton buttonToAdd = new JRadioButton(new Integer(i).toString(), false);
			buttonToAdd.setActionCommand(new Integer(i).toString());
			bg.add(buttonToAdd);
			radioPanel.add(buttonToAdd);
		}
		recomPanel.add(radioPanel);
		JLabel summaryLabel = new JLabel("Summary Recommendation");
		summaryArea.setBorder(BorderFactory.createLineBorder(Color.black));
		recomPanel.add(summaryLabel);
		recomPanel.add(summaryArea);
		
		
		buttonsPanel.add(viewButton);
		buttonsPanel.add(submitButton);
		buttonsPanel.add(assignReviewerButton);
		buttonsPanel.add(logoutButton);
		p.add(manTabel.getTableHeader());
		p.add(manTabel);
		p.add(recomPanel);
		p.add(buttonsPanel);
		add(p);
		pack();
		setVisible(true);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		actionButton();
	}
	
	/**
	 * Add actions to buttons.
	 */
	private void actionButton() {
		submitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int x = manTabel.getSelectedRow();
				String author = (String) manuscriptData[x][1];
				String manTitle = (String) manuscriptData[x][0];
				SubProgramChair spc = new SubProgramChair(id, inteactor);
				String rating = bg.getSelection().getActionCommand();
				String comment = summaryArea.getText();
				spc.submitRecommendation(id, conference, author, manTitle, rating, comment);
			}
		});
		
		assignReviewerButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int x = manTabel.getSelectedRow();
				String author = (String) manuscriptData[x][1];
				String manTitle = (String) manuscriptData[x][0];
				AssignReviewerView arv = new AssignReviewerView(id, author, manTitle, conference, inteactor);
				arv.createAssignReviewerView();
			}
		});
		
		
		logoutButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
				LogInFrame loginframe = new LogInFrame(inteactor);
				loginframe.setVisible(true);
			}
		});
	}
}
